const {
	db,
	dbCmd,
	result_success,
	result_error,
	appletInfoCollection,
	collectionCollection,
	userCollection,
	getCommonTime
} = require('../../common/constants')
module.exports = async function(params = {}) {
	const token = this.getUniIdToken()
	if (!token) {
		return result_error('登录失效,请重新登录')
	}
	let payload = await this.uniID.checkToken(token)
	if (payload.code) {
		return payload
	}
	let type = params.type
	let cUser = await userCollection.doc(payload.uid).get()
	cUser = cUser.data[0]
	//获取当前应用
	let appletInfoData = await appletInfoCollection.doc(cUser.app_id).get()
	appletInfoData = appletInfoData.data[0]
	
	let cDate = getCommonTime();
	let cYear = cDate.now.year;
	let cMonth = cDate.now.month;
	let cDay = cDate.now.day;
	let shareCount = 1
	let shareType = 'recharge'
	let shareLogs = await db.collection('wmzmall-share-logs').where({
		shop_id: appletInfoData._id,
		year: cYear,
		month: cMonth,
		day: cDay,
		type: shareType
	}).get()
	shareLogs = shareLogs.data[0]
	if (!shareLogs) {
		await db.collection('wmzmall-share-logs').add({
			shop_id: appletInfoData._id,
			year: cYear,
			month: cMonth,
			day: cDay,
			type: shareType,
			count: 1
		})
	} else {
		await db.collection('wmzmall-share-logs').doc(shareLogs._id).update({
			count: dbCmd.inc(1)
		})
		if (shareLogs.count >= shareCount) {
			return result_error('每天限制分享' + shareCount + '次,如需发送给多人,请从好友转发..')
		}
	} 
	
	let app_id = appletInfoData._id
	let wechat_id = appletInfoData.wechat.gh_id
	let alipay_id = appletInfoData.alipay.app_id
	let share_title = "储值"
	let share_summary = ''
	let share_path = '/sub-user/pages/wallet/wallet'
	let share_image = 'https://mp-d84e928e-2b27-4fa9-b240-63574c62e78a.cdn.bspapp.com/QQ截图20240408210058.png'
	let share_web = "https://mall.web.wmeizi.com/skipapplet.html?appId=" + alipay_id



	let recharge = await db.collection('wmzmall-recharge').where({
		shop_id: app_id
	}).get()
	recharge = recharge.data
	for (var i in recharge) {
		share_title = '储值' + recharge[i].money + "元,到账:" + recharge[i].arrival + "元"
		if (recharge[i].share_title) {
			share_title = recharge[i].share_title
		}
		if (recharge[i].share_image) {
			share_image = recharge[i].share_image
		}
		if (recharge[i].share_summary) {
			share_summary = recharge[i].share_summary
		}
		recharge[i].share_title = share_title
		recharge[i].share_image = share_image
		recharge[i].share_summary = share_summary
		recharge[i].wechat_id = wechat_id
		recharge[i].share_path = share_path
		recharge[i].share_web = share_web
		recharge[i].WXSceneSession = true
		recharge[i].WXSceneTimeline = false
		recharge[i].checked = false
	}
	
	return result_success(recharge)
}